package com.weitu.archives.personArchives.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.archives.personArchives.entity.SecurityMeetingLedger;
import com.weitu.archives.personArchives.service.SecurityMeetingLedgerService;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/5/15.
 */
@Controller
@RequestMapping("archives/personArchives/securityMeetingLedger")
public class SecurityMeetingLedgerController extends BaseController{
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"archives/personArchives/securityMeetingLedger";
    private final SecurityMeetingLedgerService securityMeetingLedgerService;
    private final AccessoryUtilService accessoryUtilService;
    @Autowired
    public SecurityMeetingLedgerController(SecurityMeetingLedgerService securityMeetingLedgerService,AccessoryUtilService accessoryUtilService){
        this.securityMeetingLedgerService=securityMeetingLedgerService;
        this.accessoryUtilService=accessoryUtilService;
    }

    /**
     * 页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.securityMeetingLedgerService.queryCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);
        PageView pageView=dataGridPagination.getPageView();
        paramMap.put("pageView",pageView);
        List<SecurityMeetingLedger> securityMeetingLedgerList=this.securityMeetingLedgerService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(securityMeetingLedgerList);
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("addUserId",BaseController.getSessionUser().getId());
        map.put("addUserName",BaseController.getSessionUser().getRealName());
        map.put("addDeptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("addDeptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime("yyyy-MM-dd"));
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }

    /**
     * 添加
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(module = "安全会议台账",methods = "添加安全会议台账")
    public AjaxResult addAjax(SecurityMeetingLedger securityMeetingLedger){
        securityMeetingLedger.setId(IDGenerator.getId());
        boolean flag=this.securityMeetingLedgerService.insertSecurityMeetingLedger(securityMeetingLedger);
        if(flag){
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.fail("添加失败");
        }
    }

    /**
     * 编辑页面
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        SecurityMeetingLedger securityMeetingLedger=this.securityMeetingLedgerService.querySecurityMeetingLedgerById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("securityMeetingLedger",securityMeetingLedger);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }
    /**
     * 详情页面
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView detail(String id){
        SecurityMeetingLedger securityMeetingLedger=this.securityMeetingLedgerService.querySecurityMeetingLedgerById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("securityMeetingLedger",securityMeetingLedger);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/detail");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @SystemLog(module = "安全会议台账",methods = "编辑安全会议台账")
    public AjaxResult editAjax(SecurityMeetingLedger securityMeetingLedger){
        securityMeetingLedger.setUpdateUserId(BaseController.getSessionUser().getId());
        securityMeetingLedger.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        securityMeetingLedger.setUpdateTime(DateTimeUtil.formatterDateTime());
        boolean flag=this.securityMeetingLedgerService.updateSecurityMeetingLedger(securityMeetingLedger);
        if(flag){
            return AjaxResult.success("编辑成功");
        }else {
            return AjaxResult.fail("编辑失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @SystemLog(module = "安全会议台账",methods = "删除安全会议台账")
    public AjaxResult delete(String id){
        int count=this.securityMeetingLedgerService.deleteSecurityMeetingLedger(id);
        if(count==1){
            return AjaxResult.success("删除");
        }else{
            return AjaxResult.fail("删除");
        }
    }
}
